package com.xzx.study;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutFileActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = OutFileActivity.class.getSimpleName();

    private EditText out_file_name;
    private EditText out_file_content;

    private Button out_file_save;
    private Button out_file_read;
    private Button out_file_save2;
    private Button out_file_read2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_file);

        out_file_name = findViewById(R.id.out_file_name);
        out_file_content = findViewById(R.id.out_file_content);

        out_file_read = findViewById(R.id.out_file_read);
        out_file_save = findViewById(R.id.out_file_save);
        out_file_read2 = findViewById(R.id.out_file_read2);
        out_file_save2 = findViewById(R.id.out_file_save2);

        out_file_read.setOnClickListener(this);
        out_file_save.setOnClickListener(this);
        out_file_read2.setOnClickListener(this);
        out_file_save2.setOnClickListener(this);

        if (AndPermission.hasAlwaysDeniedPermission(this, Permission.WRITE_EXTERNAL_STORAGE)) {
            AndPermission.with(this).runtime().permission(Permission.WRITE_EXTERNAL_STORAGE).start();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.out_file_save) {
            // 判断sd卡的状态
            if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                Toast.makeText(this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
                return;
            }

            File file = getExternalFilesDir(null);
            Log.i(TAG, "file=" +file.getAbsolutePath());

            String fileName = out_file_name.getText().toString();
            String content = out_file_content.getText().toString();
            FileOutputStream fileOutputStream;
            try {
                fileOutputStream = new FileOutputStream(file.getAbsoluteFile() + "/" + fileName);
                fileOutputStream.write(content.getBytes());
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } else if (v. getId() == R.id.out_file_read) {
            // 判断sd卡的状态
            if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                Toast.makeText(this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
                return;
            }

            File file = getExternalFilesDir(null);
            Log.i(TAG, "file=" +file.getAbsolutePath());

            String fileName = out_file_name.getText().toString();
            FileInputStream fileInputStream;
            try {
                fileInputStream = new FileInputStream(file.getAbsoluteFile() + "/" + fileName);
                byte[] bytes = new byte[1024 * 1024];
                int len = -1;
                StringBuilder builder = new StringBuilder();
                while ((len=fileInputStream.read(bytes)) != -1) {
                    builder.append(new String(bytes, 0 ,len));
                }
                fileInputStream.close();
                out_file_content.setText(builder.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(this, "读取成功", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.out_file_save2) {
            // 判断sd卡的状态
            if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                Toast.makeText(this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
                return;
            }

            String fileName = out_file_name.getText().toString();
            String content = out_file_content.getText().toString();
            FileOutputStream fileOutputStream;
            try {
                String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
                Log.i(TAG, "sdcardPath=" + sdCardPath);
                File file = new File(sdCardPath + "/xzx");
                if (!file.exists()) {
                    file.mkdirs();
                }
                fileOutputStream = new FileOutputStream(file.getAbsoluteFile() + "/" + fileName);
                fileOutputStream.write(content.getBytes());
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.out_file_read2) {
            // 判断sd卡的状态
            if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                Toast.makeText(this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
                return;
            }

            String fileName = out_file_name.getText().toString();
            FileInputStream fileInputStream;
            try {
                String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
                Log.i(TAG, "sdcardPath=" + sdCardPath);
                File file = new File(sdCardPath + "/xzx");
                if (!file.exists()) {
                    file.mkdirs();
                }

                fileInputStream = new FileInputStream(file.getAbsoluteFile() + "/" + fileName);
                byte[] bytes = new byte[1024 * 1024];
                int len = -1;
                StringBuilder builder = new StringBuilder();
                while ((len=fileInputStream.read(bytes)) != -1) {
                    builder.append(new String(bytes, 0 ,len));
                }
                fileInputStream.close();
                out_file_content.setText(builder.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(this, "读取成功", Toast.LENGTH_SHORT).show();
        }
    }
}
